package com.rdk.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.rdk.entity.CheckRequstParam;
import com.rdk.entity.CheckResponseEntity;
import com.rdk.entity.KwsParam;
import com.rdk.entity.KwsRequestParam;
import com.rdk.entity.KwsResponseEntity;
import com.rdk.entity.KwsResult;
import com.rdk.entity.PSKeyWord;
import com.rdk.exception.ServiceException;
import com.rdk.service.PSKeyWordService;
import com.rdk.utils.GsonUtil;
import com.rdk.wechat.common.Constant;
import com.rdk.wechat.request.impl.BaseRequest;
import com.rdk.wechat.util.JSONutils;

/**
 * @ClassName: PSKeyWordServiceImpl
 * @Description: TODO(杩欓噷鐢ㄤ竴鍙ヨ瘽鎻忚堪杩欎釜绫荤殑浣滅敤)
 * @author dinghao
 * @date 2016骞�鏈�鏃�涓婂崍11:48:33
 * 
 */
public class PSKeyWordServiceImpl extends BaseRequest implements PSKeyWordService {

	public PSKeyWordServiceImpl() {
		super(null);
	}

	/**
	 * @Title: 鏌ヨ鎵�湁鍏抽敭瀛椾俊鎭� @Description: 鏌ヨ鎵�湁涓汉鍏抽敭瀛椾俊鎭� @param
	 *         KwsRequestParam kwsRequestParam,String key
	 *         锛堥〉鏁帮紝姣忛〉鏉℃暟锛岀敤鎴凤紝浠ょ墝锛� @return PSResult
	 *         <Keyword4Person> 缁撴灉瀹炰綋绫� @throws
	 */
	public KwsResult getAllkws(KwsRequestParam kwsRequestParam, String key) {
		Map classMap = new HashMap();
		classMap.put("data", PSKeyWord.class);
		we.setWebFlag("publicSentimentUrl");
		we.setServiceName(Constant.PublicSentimentServiceName.KEYWORD);
		we.setOpName(Constant.PublicSentimentOpName.ALLKWS);

		String jsonStr = null;
		String url = this.getUrl(we);
		String jsonParma = JSONutils.ObjectToJson(kwsRequestParam);
		KwsResult jsonList = null;
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(url);
			httppost.addHeader("usergid", key);
			StringEntity myEntity = new StringEntity(jsonParma, ContentType.APPLICATION_JSON);
			httppost.setEntity(myEntity);
			HttpResponse response = httpclient.execute(httppost);
			int stateCode = response.getStatusLine().getStatusCode();
			if (stateCode == 200) {
				HttpEntity entity = response.getEntity();
				/*jsonStr = EntityUtils.toString(entity, "UTF-8");*/
			/*	System.out.println("----------------query keyword json : " + jsonStr);*/
				
				Gson gson = new Gson();
            	KwsResponseEntity backbean = gson.fromJson(EntityUtils.toString(entity,"UTF-8"), KwsResponseEntity.class);
            	System.out.println(backbean.getData());
            	
            	
            	jsonList = new KwsResult();
            	if (null != backbean.getData()){
            		List<PSKeyWord> psList = GsonUtil.jsonToList(backbean.getData().toString(), new TypeToken<List<PSKeyWord>>(){}.getType());
            		jsonList.setData(psList);
            	}
            	
            	System.out.println(jsonList);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return jsonList;
	}

	/**
	 * @Title: 鎵归噺淇敼鍏抽敭瀛椾俊鎭� @Description: 鏌ヨ鎵�湁涓汉鍏抽敭瀛椾俊鎭� @param KwsParam
	 *         kwsParam (鐢ㄦ埛锛屼护鐗岋紝鍏抽敭瀛椾俊鎭� @return PSResult
	 *         <Keyword4Person> 缁撴灉瀹炰綋绫� @throws
	 */
	public KwsResponseEntity batchupdataPersonalKwsByUserId(KwsParam kwsParam) {
		KwsRequestParam kp = new KwsRequestParam();
		kp.setUserAgentNo(kwsParam.getUserAgentNo());
		if (kwsParam != null) {
			if (kwsParam.getKwds() != null) {
				kp.setQuery(JSONutils.ObjectToJson(kwsParam.getKwds()));
			} else {
				kp.setQuery("[]");
			}
		}
		we.setWebFlag("publicSentimentUrl");
		we.setServiceName(Constant.PublicSentimentServiceName.KEYWORD);
		we.setOpName(Constant.PublicSentimentOpName.UPDATEKWS);
		String jsonStr = null;
		String url = this.getUrl(we);
		String jsonParma = JSONutils.ObjectToJson(kp);
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(url);
			httppost.addHeader("usergid", kwsParam.getKey());
			StringEntity myEntity = new StringEntity(jsonParma, ContentType.APPLICATION_JSON);
			httppost.setEntity(myEntity);
			HttpResponse response = httpclient.execute(httppost);
			int stateCode = response.getStatusLine().getStatusCode();
			if (stateCode == 200) {
				HttpEntity entity = response.getEntity();
				jsonStr = EntityUtils.toString(entity, "UTF-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return JSONutils.jsonToBean(jsonStr, KwsResponseEntity.class);
	}

	public static void main(String[] args) throws ServiceException {
		CheckUserServiceImpl ch = new CheckUserServiceImpl();
		CheckRequstParam cp = new CheckRequstParam();
		cp.setTimeout(1000);
		cp.setType(0);
		cp.setUserAgentNo("15850593063");
		CheckResponseEntity ur = ch.getUserAccount(cp);
		System.out.println(ur.getFirstGID());
		KwsRequestParam kp = new KwsRequestParam();
		kp.setType(0);
		kp.setTimeout(1000);
		kp.setUserAgentNo("15850593063");
		kp.setLimit(10);
		kp.setCurrentPage(1);
		PSKeyWordServiceImpl pw = new PSKeyWordServiceImpl();
		KwsResult pr = pw.getAllkws(kp, ur.getFirstGID());
	}
}
